﻿using Fuse8.ContentManagementFramework.Administration.Attributes;
using Fuse8.ContentManagementFramework.BizFacade.Services;
using Fuse8.ContentManagementFramework.Domain;
using System.ComponentModel.DataAnnotations;

namespace Fuse8.ContentManagementFramework.Administration.Models
{
    public class CmsContentTypeModel : CmsSiteAwareModel
    {
        [GlobalizedRequired("Please enter content type name", "cms_Content")]
        public string ContentTypeName { get; set; }

        public string Description { get; set; }

        public bool IsSystemType { get; set; }

        public bool IsSupportedLogicalDeletion { get; set; }

        public bool IsSupportedRevisions { get; set; }

        public bool IsSupportedWorkflow { get; set; }

        public bool IsSummaryEditable { get; set; }

        public bool IsBodyEditable { get; set; }

        public CmsContentType ToDomainEntity()
        {
            var result = new CmsContentTypeService().FindOne(Id) ?? new CmsContentType();
            result.ContentTypeName = ContentTypeName;
            result.Description = Description;
            result.IsSystemType = IsSystemType;
            result.IsSupportedLogicalDeletion = IsSupportedLogicalDeletion;
            result.IsSupportedRevisions = IsSupportedRevisions;
            result.IsSupportedWorkflow = IsSupportedWorkflow;
            result.IsSummaryEditable = IsSummaryEditable;
            result.IsBodyEditable = IsBodyEditable;
            result.SiteId = SiteId;
            result.SiteGroupId = SiteGroupId;
            return result;
        }

        public void Init(int id)
        {
            var item = new CmsContentTypeService().FindOne(id);
            Id = id;
            if (item != null)
            {
                ContentTypeName = item.ContentTypeName;
                Description = item.Description;
                IsSystemType = item.IsSystemType;
                IsSupportedLogicalDeletion = item.IsSupportedLogicalDeletion;
                IsSupportedRevisions = item.IsSupportedRevisions;
                IsSupportedWorkflow = item.IsSupportedWorkflow;
                IsSummaryEditable = item.IsSummaryEditable;
                IsBodyEditable = item.IsBodyEditable;
                SiteId = item.SiteId;
                SiteGroupId = item.SiteGroupId;
            }
            else
            {
                IsBodyEditable = true;
                IsSummaryEditable = true;
                IsSupportedRevisions = true;
            }
        }
    }
}